<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Administration</title>
        <?
        require_once("../config.php");
        ?>
        <script type="text/javascript" src="./lib/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="./lib/fileuploader.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                
                var uploader = new qq.FileUploader({
                    element: document.getElementById('portrait-uploader'),
                    action: './webservice.php',
                    debug: true,
                    onSubmit: function(id, fileName)
                    {
                        $('#portrait-img').attr('src', "./res/layout/loading.gif");
                    },
                    onComplete: function(id, fileName, responseJSON)
                    {
                        //debugger;
                        $('#portrait-img').attr('src', "./res/img_char/"+fileName);
                        $('#portrait').val(fileName);
                        
                    }
                });
                
                var id = <? echo $_GET['param']; ?>;
                var result = jQuery.ajax({
                    url: "./webservice.php",       
                    cache: false,
                    async: false,
                    data: ({
                        method: "getCharacter",
                        id: id
                    }),
                    success: function(message) {
                        var jObj = JSON.parse(message);
                        return jObj;
                        //$("#preloader").hide();
                    },
                    error: function(req,status,error) {
                        jQuery("#content").empty().append(error);
                        //$("#preloader").hide();
                        alert(error);
                    }
                });
                var character = JSON.parse(result.responseText);
                //$('#surname').val(surname);
                //$('#lastname').val(lastname);
                $('#_cid').val(character["_cid"]);
                $('#_oid').val(character["_oid"]);
                $('#_rid').val(character["_rid"]);
                $('#age').val(character["age"]);
                $('#gender').val(character["gender"]);
                $('#type').val(character["type"]);
            });
            
            function saveThisChar()
            {
                var character = new Array();
                character["_id"] = <? echo $_GET['param']; ?>;
                character["surname"] = $('#surname').val();
                character["lastname"] = $('#lastname').val();
                character["_cid"] = $('#_cid').val();
                character["_oid"] = $('#_oid').val();
                character["_rid"] = $('#_rid').val();
                character["age"] = $('#age').val();
                character["gender"] = $('#gender').val();
                character["profession"] = $('#profession').val();
                character["race"] = $('#race').val();
                character["type"] = $('#type').val();
                character["task"] = $('#task').val();
                character["klasse"] = $('#class').val();
                character["description"] = $('#description').val();
                character["portrait"] = $('#portrait').val();
                character["status"] = $('#status').val();
                var result = updateCharacter(character);
                alert(result);
                loadTabContent('./view/admin.character.overview.php','');
            }

        </script>
    </head>
    <body>
        <?
        $character = controllerCharacter::get($_GET['param']);
        ?>
        <table border="0" class="admin">
            <thead>
            <th colspan="2">Charakter bearbeiten</th>
        </thead>
        <tr>
            <td>Vorname:</td>
            <td><input type="text" id="surname" value="<? echo $character->surname; ?>" /></td>
        </tr>
        <tr>
            <td>Nachname:</td>
            <td><input type="text" id="lastname" value="<? echo $character->lastname; ?>" /></td>
        </tr>
        <tr>
            <td><div id="portrait-uploader"></div></td>
            <td><img id="portrait-img" src="./res/img_char/<? echo $character->portrait; ?>"><input type="hidden" id="portrait" value="<? echo $character->portrait; ?>"></td>
        </tr>
        <tr>
            <td>Twink:</td>
            <td><select id="_cid">
                    <option value="NULL">Hauptcharakter</option>
                    <?
                    $sql = "SELECT * FROM `character` WHERE `_cid` = 0";
                    $result = MySql::query($sql);
                    controllerCharacter::getMainChars();
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Organisation:</td>
            <td><select id="_oid">
                    <option value="">keine</option>
                    <?
                    $sql = "SELECT * FROM `organisation`";
                    $result = MySql::query($sql);
                    while ($row = mysql_fetch_object($result)) {
                        echo "<option value=" . $row->_id . ">" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . "</option>";
                    }
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Rang:</td>
            <td><select id="_rid">
                    <option value="">keiner</option>
                    <?
                    $sql = "SELECT * FROM `rank`";
                    $result = MySql::query($sql);
                    while ($row = mysql_fetch_object($result)) {
                        echo "<option value=" . $row->_id . ">" . htmlentities($row->name, ENT_QUOTES, 'UTF-8') . "</option>";
                    }
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Alter:</td>
            <td><input type="text" id="age" value="0" /></td>
        </tr>
        <tr>
            <td>Geschlecht:</td>
            <td><select id="gender">
                    <option value="male">Männlich</option>
                    <option value="female">Weiblich</option>
                </select></td>
        </tr>
        <tr>
            <td>Herstellungsberuf:</td>
            <td><select id="profession">
                    <?
                    controllerProfession::getOptionList($character->id);
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Rasse:</td>
            <td><select id="race">
                    <?
                    controllerRace::getOptionList($character->id);
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Charakter-Typ:</td>
            <td><select id="type">
                    <option value="sc">Spieler-Charakter</option>
                    <option value="nsc">Nicht-Spieler-Charakter</option>
                </select></td>
        </tr>
        <tr>
            <td>Status:</td>
            <td><input type="text" id="status" value="<? echo $character->status; ?>" /></td>
        </tr>
        <tr>
            <td>Gruppenfunktion:</td>
            <td><input type="text" id="task" value="<? echo $character->task; ?>" /></td>
        </tr>
        <tr>
            <td>Klasse:</td>
            <td><select id="class">
                    <?
                    controllerClass::getOptionList($character->id);
                    ?>
                </select></td>
        </tr>
        <tr>
            <td>Kurzbeschreibung:</td>
            <td><textarea id="description" cols="40" rows="4"><? echo $character->description; ?></textarea></td>
        </tr>
        <tr>
            <td colspan="2"><input type="button" onclick="saveThisChar()" value="Speichern"></td>
        </tr>
    </table>
    <!--        </form>-->
</body>
</html>
